<%@page language="java" pageEncoding="UTF-8"
%><%@page import="kr.co.sdworld.api.apns.model.ApnsApp"
%><%@page import="java.util.Iterator"
%><%@page import="kr.co.sdworld.api.apns.model.base.Model"
%><%@page import="java.util.List"
%><%@page import="kr.co.sdworld.api.apns.service.base.ListResult"
%><%@page import="kr.co.sdworld.webs.ServiceContext"
%><%@page import="kr.co.sdworld.api.apns.model.ApnsLogin"
%><%
	ApnsLogin login = (ApnsLogin)session.getAttribute("login");
%>
<html>
<head>
	<jsp:include page="/WEB-INF/jsp/ApnsHeader.jsp" />
	<script type="text/javascript">
	function doPush()
	{
		var buttonTag = $("#buttonPannel").html();
		$("#buttonPannel").html("전송중...");
		
		APNS.util.doAjax({
			url: APNS.context.getContextUrl( "/push.sd"),
			data: {
				appId:$("#pushSampleTest [name='appId']").val(),
				deviceToken:$("#pushSampleTest [name='deviceToken'] option:selected").val(),
				message:$( "#pushSampleTest [name='message']").val(),
				sound:$( "#pushSampleTest [name='sound']").val()
			},
			complete: function(req, status) {
				if( status == "success") 
				{
					var resp = JSON.parse( req.responseText);
					if( resp.result == "success")
					{
						alert( "success");
					}
				}
				
				$("#buttonPannel").html(buttonTag);
			}
		});
	}
	
	function doSaveToken() 
	{
		//var appId = $( "#saveTokenSampleTest [name='appId']").val();
		//var deviceToken = $( "#saveTokenSampleTest [name='deviceToken']").val();
		
		APNS.util.doAjaxForm( "saveTokenSampleForm", {
			url: APNS.context.getContextUrl( "/propertySet/deviceTokenSampleSaveWithSession.sd"),
			complete: function(req, status) {
				alert(req.responseText);
			}
		});
	}
	
	function doLoadAppList()
	{
		APNS.util.doAjax({
			url: APNS.context.getContextUrl( "/ajaxList.sd"),
			data: {
				funcId:"appList"
			},
			complete:function(req, status) {
				if(status == "success")
				{
					var result = JSON.parse(req.responseText);
					var appList = result.appList.list;
					var $selectAppId = $("#pushSampleTest select[name='appId']");
					var $uploadAppId = $("#certUploadForm select[name='appId']");
					var $tokkenAppId = $("#saveTokenSampleTest select[name='appId']");
					
					if(appList.length > 0)
					{
						for(var i = 0; i < appList.length; i++)
						{
							var appId = appList[ i].appId;
							var appName = appList[ i].appName;
							$selectAppId.append("<option value=\"" + appId + "\">" + appName + "</option>");
							$uploadAppId.append("<option value=\"" + appId + "\">" + appName + "</option>");
							$tokkenAppId.append("<option value=\"" + appId + "\">" + appName + "</option>");
						}
					}
					else
					{
						$selectAppId.html("<option value=\"\">등록된 앱이 없음</option>");
					}
				}
			}
		});
	}
	
	function doUploadCertFile() 
	{
		$("#certUploadForm").attr("action", APNS.context.getContextUrl("/upload.sd"));
		$("#certUploadForm").ajaxForm({
			complete: function(req, status) {
				if(status == "success")
				{
					alert("upload success!\n" + req.responseText);
				}
				else
				{
					alert("upload fail!\n" + req.responseText);
				}
				$("#certUploadForm").clearForm();
			}
		}).submit();
	}
	
	function doLoadTokenList( appId)
	{
		APNS.util.doAjax({
			url: APNS.context.getContextUrl( "/ajaxList.sd"),
			data: {
				funcId: "sampleTokenList",
				appId: appId
			},
			complete: function(req, status) {
				var jsonText = req.responseText;
				var result = eval("(" + jsonText + ")");
				var sampleTokenList = result.sampleTokenList;
				var $selectToken = $("#pushSampleTest [name='deviceToken']");
				if(sampleTokenList.length == 0)
				{
					$selectToken.html("<option value=''>등록된 토큰 없음</option>");
				}
				else
				{
					$selectToken.html("<option value=''>선택</option>");
					for( var i = 0; i < sampleTokenList.length; i++)
					{
						var sampleToken = sampleTokenList[ i];
						var deviceTokenAlias = sampleToken.deviceTokenAlias;
						var tokenText = sampleToken.deviceToken;
						if( deviceTokenAlias != null && deviceTokenAlias.length > 0)
						{
							tokenText = deviceTokenAlias;
						}
						
						$selectToken.append("<option value='" + sampleToken.deviceToken + "'>" + tokenText + "</option>");
					}
				}
			}
		});
	}
	
	$( document).ready( function(){
		$( "#saveTokenButton").click(function(){
			doSaveToken();
		});
		
		$( "#pushSampleTest select[name='appId']").change(function(){
			var appId = $( this).children( ":selected").val();
			
			if( appId == null || appId.length == 0)
				return;
			
			doLoadTokenList( appId);
		});
		
		doLoadAppList();
	});
	</script>
</head>
<body>
<jsp:include page="/WEB-INF/jsp/ApnsTopInfo.jsp" />

<div id="main">
	<!-- 테스트용으로 올려놓은 앱만 작동한다. -->
	<h2>1.Push Message Test</h2>
	<div id="pushSampleTest">
		<p>Sample로 등록된것만 작동하게 되어있음.</p>
		<ul>
			<li>AppId : 
				<select name="appId">
				</select>
			</li>
			<li>DeviceToken : 
				<select name="deviceToken" style="width:100px;">
					<option value="">App 을 선택해주세요.</option>
				</select>
			</li>
			<li>Message : <input type="text" name="message" /></li>
			<li>Sound : <input type="text" name="sound" value="default" /></li>
		</ul>
	</div>
	<div class="pannel" id="buttonPannel">
		<input id="pushButton" type="button" value="Push Message!" onclick="doPush()"/>
	</div>
	
	<h2>2.Save Token</h2>
	<div id="saveTokenSampleTest">
		<form id="saveTokenSampleForm" method="post">
		<ul>
			<li>AppId : <select name="appId"></select></li>
			<li>DeviceToken : <input type="text" name="deviceToken" /></li>
			<li>URL : http://<%= request.getServerName() + ":" + request.getServerPort() + request.getContextPath() %>/propertySet/deviceTokenSampleSave.sd
			<li>Param : <ul>
				<li>id : {로그인 아이디}</li>
				<li>pw : {로그인 암호}</li>
				<li>deviceToken : {저장하려는 토큰}</li>
				<li>deviceTokenAlias : {토큰을 표시할 이름, 없을 경우 토큰값으로 대체}</li>
			</ul></li>
			<li>Description : 전송 타입은 반드시 Post 로 해야 한다.</li>
		</ul>
		</form>
	</div>
	<div>
		<input type="button" id="saveTokenButton" value="Save Token" />
	</div>
	<h2>3.Upload Certification</h2>
	<div id="certUpload">
		<form id="certUploadForm" method="post" enctype="multipart/form-data">
		<ul>
			<li>AppId : <select name="appId"></select></li>
			<li>인증서암호 : <input type="password" name="certPass" /></li>
			<li>파일 : <input type="file" name="certFile" /></li>
		</ul>
		</form>
	</div>
	<div class="pannel">
		<input id="certUploadButton" type="button" value="Upload" onclick="doUploadCertFile();" />
	</div>
</div>
</body>
</html>